﻿using System;
using System.Data;
using System.Data.Common;
using System.Reflection;

/// <summary>
/// Populate all action with Category
/// </summary>
public static class EF_Category
{
	static EF_Category()
	{
	}
	// Create new category
	public static void CreateCategory(EF_CategoryProperty category)
	{
		DbCommand cmd = GenericDataAccess.CreateCommand();
		cmd.CommandText = "EF_CreateCategory";

		DbParameter CateParam = cmd.CreateParameter();
		CateParam.ParameterName = "@UserID";
		CateParam.Value = category.UserID;
		cmd.Parameters.Add(CateParam);

		CateParam = cmd.CreateParameter();
		CateParam.ParameterName = "@Name";
		CateParam.Value = category.Name;
		cmd.Parameters.Add(CateParam);

		CateParam = cmd.CreateParameter();
		CateParam.ParameterName = "@Description";
		CateParam.Value = category.Description;
		cmd.Parameters.Add(CateParam);

		CateParam = cmd.CreateParameter();
		CateParam.ParameterName = "@Status";
		CateParam.Value = category.Status;
		cmd.Parameters.Add(CateParam);

		GenericDataAccess.ExecuteCommand(cmd);
	}

	// Get all categories
	public static DataTable GetCategory (int DescriptionLength = 25) {
		DbCommand cmd = GenericDataAccess.CreateCommand();
		cmd.CommandText = "EF_GetCategory";

		DbParameter CateParam = cmd.CreateParameter();
		CateParam.ParameterName = "@DescriptionLength";
		CateParam.Value = DescriptionLength;
		cmd.Parameters.Add(CateParam);

		return GenericDataAccess.ExecuteSelectCommand(cmd);
	}

	​​// Update category
	public static bool UpdateCategory(EF_CategoryProperty category)
	{
		DbCommand cmd = GenericDataAccess.CreateCommand();
		cmd.CommandText = "EF_UpdateCategoryById";

		DbParameter CateParam = cmd.CreateParameter();
		CateParam.ParameterName = "@CategoryID";
		CateParam.Value = category.CategoryID;
		cmd.Parameters.Add(CateParam);

		CateParam = cmd.CreateParameter();
		CateParam.ParameterName = "@Name";
		CateParam.Value = category.Name;
		cmd.Parameters.Add(CateParam);

		CateParam = cmd.CreateParameter();
		CateParam.ParameterName = "@Description";
		CateParam.Value = category.Description;
		cmd.Parameters.Add(CateParam);

		CateParam = cmd.CreateParameter();
		CateParam.ParameterName = "@Status";
		CateParam.Value = category.Status;
		cmd.Parameters.Add(CateParam);

		if (GenericDataAccess.ExecuteCommand(cmd))
		{
			return true;
		}
		return false;
	}

	// Delete category by name
	public static bool DeleteCategory(string categoryId)
	{
		DbCommand cmd = GenericDataAccess.CreateCommand();
		cmd.CommandText = "EF_DeleteCategory";

		DbParameter CateParam = cmd.CreateParameter();
		CateParam.ParameterName = "@CategoryID";
		CateParam.Value = categoryId;
		CateParam.DbType = DbType.Int32;
		cmd.Parameters.Add(CateParam);

		if (GenericDataAccess.ExecuteCommand(cmd))
		{
			return true;
		}
		return false;
	}
}